<template>
  <div class="apply-return-container">
    <div class="info-container">
      <div class="info-item" v-for="(item, index) in good.db1" :key="index">
        <div class="left-container">
          <img :src="imgUrl + item.indexpic" alt />
        </div>
        <div class="right-container">
          <div class="price">$ {{ item.price }}</div>
          <div class="name">{{ item.title }}</div>
          <div class="order-num">{{ orderno }}</div>
        </div>
      </div>
    </div>
    <div class="reason-container">
      <div class="header-container">
        <div class="title">退款/换货原因</div>
        <!-- <div class="right-container">
          <div class="right-item">退款</div>
          <div class="right-item active">换货</div>
        </div> -->
      </div>
      <div class="main">
        <textarea rows="5" placeholder="请输入退款/换货原因" v-model="remark"></textarea>
      </div>
    </div>
    <div class="img-container">
      <div class="title">上传图片</div>
      <van-uploader v-model="fileList" multiple :max-count="3" :before-read="beforeRead" :before-delete="beforeDelete"/>
    </div>
    <div class="ok-btn" @click="ok">
      提交
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      remark: null,
      good: {},
      fileList: [],
      photos: []
    };
  },
  computed: {
    imgUrl () {
      return this.$imgUrl
    },
    orderno () {
      return this.$route.query.orderno
    }
  },
  mounted () {
    this.getGoodDetails()
  },
  methods: {
    async getGoodDetails () {
      const result = await this.$fetchGet('/home/member/orderview', {
        orderno: this.orderno
      })
      if (!result.success) {
        this.$toast(result.msg)
        return
      }
      this.good = result.data
    },
    async ok () {
      if (!this.remark) {
        this.$toast('请输入退款/换货原因')
        return
      }
      this.$dialog.confirm({
        title: '温馨提示',
        message: '是否提交退款/换货？',
        beforeClose: async (action, done) => {
          if (action === 'cancel') {
            done()
            return
          }
          const result = await this.$fetchPost('/home/member/refund', {
            orderno: this.orderno,
            remark: this.remark,
            photos: this.photos
          })
          if (!result.success) {
            this.$toast(result.msg)
            return
          }
          this.$toast('提交成功')
          done()
          this.$router.push('/my')
        }
      })
    },
    async beforeRead (file) {
      const formData = new FormData();
      formData.append("file", file);
      const res = await this.$fetchPost("/home/file/upload", formData);
      this.photos.push(res.info)
      return false
    },
    beforeDelete (e, i) {
      this.photos.splice(i.index, 1)
      return true
    }
  }
};
</script>

<style lang="scss">
.apply-return-container {
  .info-container {
    margin-top: 50px;
    box-shadow: 0 0 10px #888;
    background: #ffffff;
    padding: 30px;
    .info-item {
      display: flex;
    }
    .left-container {
      width: 150px;
      height: 150px;
    }
    .right-container {
      flex: 1;
      margin-left: 50px;
      .price {
        font-size: 40px;
        color: #ff3b3b;
        font-weight: bold;
      }
      .name {
        font-size: 25px;
        margin-top: 20px;
        color: #646464;
      }
      .order-num {
        margin-top: 10px;
        font-size: 25px;
        color: #646464;
      }
    }
  }
  .reason-container {
    margin-top: 50px;
    box-shadow: 0 0 10px #888;
    background: #ffffff;
    padding: 30px;
    .header-container {
      display: flex;
      .title {
        font-size: 30px;
        font-weight: bold;
        color: #545454;
      }
      .right-container {
        flex: 1;
        text-align: right;
        .right-item {
          display: inline-block;
          margin-left: 30px;
          padding: 10px 30px;
          border-radius: 30px;
          color: #FF4949;
          border: 1PX solid #FF4949;
          &.active {
            color: #fff;
            background: linear-gradient(to right, #fc8f04, #f71c19);
          }
        }
      }
    }
    .main {
      margin-top: 30px;
      textarea {
        width: 100%;
        border: none;
      }
      // width: calc(100vw - 60px);
    }
  }
  .img-container {
    margin-top: 50px;
    box-shadow: 0 0 10px #888;
    background: #ffffff;
    padding: 30px;
    .title {
      font-size: 30px;
      font-weight: bold;
      color: #545454;
      margin-bottom: 20px;
    }
    .van-uploader__preview {
      margin-right: 30px;
    }
    .van-uploader__preview-image, .van-uploader__upload {
      width: 150px;
      height: 150px;
    }
  }
  .ok-btn {
    margin-top: 50px;
    width: 80vw;
    margin-left: 10vw;
    text-align: center;
    line-height: 100px;
    background: linear-gradient(to right, #fc8f04, #f71c19);
    border-radius: 50px;
    color: #fff;
    font-size: 35px;
    font-weight: bold;
    letter-spacing: 10px;
  }
}
</style>